Imports System.Xml
Imports System.IO
Imports System.Text
Imports SQLtoASPX.Globals
Imports System.Data.SqlClient

Public Class frmMaster

    Public Servers As New List(Of ConnectServerItem)

    Private mCurrentCSI As ConnectServerItem
    Public Property CurrentCSI() As ConnectServerItem
        Get
            Return mCurrentCSI
        End Get
        Set(ByVal value As ConnectServerItem)
            mCurrentCSI = value
        End Set
    End Property


    Private Sub frmMaster_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveCSI()
    End Sub

    Private Sub CheckBackupDirectory()
        If System.IO.Directory.Exists(Environment.CurrentDirectory & "\Backup") = False Then
            System.IO.Directory.CreateDirectory(Environment.CurrentDirectory & "\Backup")
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        CheckBackupDirectory()

        'xtOutput.Text = "Test"
        CreateServerNode()
        LoadCodeTypeList()
        'LoadDatabaseType()
        LoadConnectionStringTypeList()
        LoadCommentLevelList()
        LoadGridCodingStyle()
        'LoadButtonStyle()
        'LoadClassGenerationCodingStyle()
        LoadPaging()
        LoadKeyPassing()
        'LoadDBAccessMethod()
        LoadProjectList()

        MasterForm = Me


    End Sub

    Private Sub LoadKeyPassing()
        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = "Request"
        dr("Value") = "Request - User Sees the Key"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Session"
        dr("Value") = "Session - User Does not See the Key"
        dt.Rows.Add(dr)
        dr = dt.NewRow

        dr("Key") = "Inline"
        dr("Value") = "Inline - All on the same page"
        dt.Rows.Add(dr)

        cboKeyPassing.ValueMember = "Key"
        cboKeyPassing.DisplayMember = "Value"
        cboKeyPassing.DataSource = dt

    End Sub

    Private Sub LoadGridCodingStyle()


        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = "GridView"
        dr("Value") = "GridView"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Repeater"
        dr("Value") = "Repeater"
        dt.Rows.Add(dr)


        dr = dt.NewRow
        dr("Key") = "CodeBehind"
        dr("Value") = "CodeBehind"
        dt.Rows.Add(dr)

        cboGridCodingStyle.ValueMember = "Key"
        cboGridCodingStyle.DisplayMember = "Value"
        cboGridCodingStyle.DataSource = dt

        cboGridCodingStyle.DataSource = dt
    End Sub


    Private Sub LoadPaging()
        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = "None"
        dr("Value") = "None"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Basic"
        dr("Value") = "Basic Paging(GridView Only)"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "HighSpeed"
        dr("Value") = "SQL 2005+ High Speed (Grid/Repeater/CodeBehind)"
        dt.Rows.Add(dr)

        cboPaging.ValueMember = "Key"
        cboPaging.DisplayMember = "Value"
        cboPaging.DataSource = dt
    End Sub

    Private Sub LoadCodeTypeList()
        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = "VB2005"
        dr("Value") = "VB - 2005/2008"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "CS2005"
        dr("Value") = "C# - 2005/2008"
        dt.Rows.Add(dr)

        cboCodeType.ValueMember = "Key"
        cboCodeType.DisplayMember = "Value"
        cboCodeType.DataSource = dt
    End Sub

    Private Sub LoadConnectionStringTypeList()
        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = "Web.Config Connection Strings"
        dr("Value") = "Web.Config Connection Strings"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Web.Config App Settings"
        dr("Value") = "Web.Config App Settings"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Global Class Variable"
        dr("Value") = "Global Class Variable"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Session"
        dr("Value") = "Session"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = "Application"
        dr("Value") = "Application"
        dt.Rows.Add(dr)

        cboConnectionStringType.ValueMember = "Key"
        cboConnectionStringType.DisplayMember = "Value"
        cboConnectionStringType.DataSource = dt
    End Sub

    Private Sub LoadCommentLevelList()

        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = -1
        dr("Value") = "None"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = 1
        dr("Value") = "Basic - Best Practice"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Key") = 2
        dr("Value") = "Verbose - Detailed Commenting"
        dt.Rows.Add(dr)

        cboCommentLevel.ValueMember = "Key"
        cboCommentLevel.DisplayMember = "Value"
        cboCommentLevel.DataSource = dt
    End Sub

    Private Sub LoadProjectList()
        If System.IO.File.Exists("Project.xml") Then
            Dim doc As New XmlDocument
            doc.Load("Project.xml")
            Dim NodeList As XmlNodeList
            NodeList = doc.SelectNodes("PCG/Project")
            For Each node As XmlNode In NodeList
                Dim csi As New ConnectServerItem
                For Each a As XmlAttribute In node.Attributes
                    Select Case a.Name.ToUpper
                        Case "PROJECTNAME"
                            cboProject.Items.Add(a.Value)
                            csi.ProjectName = a.Value
                        Case "PROJECTPATH"
                            csi.ProjectPath = a.Value
                        Case "CODETYPE"
                            csi.CodeType = a.Value
                        Case "COMMENTLEVEL"
                            csi.CommentLevel = a.Value
                        Case "ADDIMPORTS"
                            csi.AddImports = a.Value
                        Case "KEYPASSING"
                            csi.KeyPassing = a.Value
                        Case "GRIDCODINGSTYLE"
                            csi.GridCodingStyle = a.Value
                        Case "SOURCECODEFILES"
                            csi.SourceCodeFiles = a.Value
                            'Case "SUPPORTFILES"
                            '    csi.SupportFiles = a.Value
                        Case "WEBCONFIGFILE"
                            csi.WebConfigFile = a.Value
                        Case "CONNECTIONSTRINGSTYLE"
                            csi.ConnectionStringStyle = a.Value
                        Case "CONNECTIONSTRINGNAME"
                            csi.ConnectionStringName = a.Value
                        Case "PROJECTPATH"
                            csi.ProjectPath = a.Value
                        Case "PAGING"
                            csi.Paging = a.Value
                        Case "SORTING"
                            csi.Sorting = a.Value
                    End Select
                Next
                Servers.Add(csi)
            Next
        End If

        If Servers.Count = 0 Then
            CurrentCSI = New ConnectServerItem
            LoadConnectServerItem(CurrentCSI)

        Else
            LoadConnectServerItem(Servers(0))
            CurrentCSI = Servers(0)
        End If
        If cboProject.Items.Count = 0 Then
            Dim csi As ConnectServerItem = CreateNewCSI()
            csi.ProjectName = "Default"
            Servers.Add(csi)
            cboProject.Items.Add(csi.ProjectName)
            SaveCSI()
            LoadConnectServerItem(CurrentCSI)
        End If
        cboProject.SelectedIndex = 0
    End Sub

    Private Sub LoadConnectServerItem(ByVal csi As ConnectServerItem)
        Try
            cboCodeType.SelectedValue = csi.CodeType
        Catch ex As Exception

        End Try

        Try
            cboCommentLevel.SelectedValue = csi.CommentLevel
        Catch ex As Exception

        End Try

        Try
            cboGridCodingStyle.SelectedValue = csi.GridCodingStyle
        Catch ex As Exception

        End Try

        Try
            cboKeyPassing.SelectedValue = csi.KeyPassing
        Catch ex As Exception

        End Try

        Try
            cboConnectionStringType.SelectedValue = csi.ConnectionStringStyle
        Catch ex As Exception

        End Try

        Try
            txtPath.Text = csi.ProjectPath
        Catch ex As Exception

        End Try

        Try
            txtConnectionString.Text = csi.ConnectionStringName
        Catch ex As Exception

        End Try

        Try
            chkIncludeSourceCode.Checked = csi.SourceCodeFiles
        Catch ex As Exception

        End Try

        Try
            chkIncludeWebConfigFile.Checked = csi.WebConfigFile
        Catch ex As Exception

        End Try

        Try
            cboPaging.SelectedValue = csi.Paging
        Catch ex As Exception

        End Try

        Try
            chkSorting.Checked = csi.Sorting
        Catch ex As Exception

        End Try
    End Sub

    Private Function CreateNewCSI() As ConnectServerItem
        Dim csi As New ConnectServerItem
        csi.CodeType = cboCodeType.SelectedValue
        csi.CommentLevel = cboCommentLevel.SelectedValue
        csi.GridCodingStyle = cboGridCodingStyle.SelectedValue
        csi.KeyPassing = cboKeyPassing.SelectedValue
        csi.ConnectionStringStyle = cboConnectionStringType.SelectedValue
        csi.ProjectPath = txtPath.Text
        csi.ConnectionStringName = txtConnectionString.Text
        csi.SourceCodeFiles = chkIncludeSourceCode.Checked
        csi.WebConfigFile = chkIncludeWebConfigFile.Checked
        csi.Paging = cboPaging.SelectedValue
        csi.Sorting = chkSorting.Checked
        csi.ProjectName = txtProjectName.Text

        Return csi
    End Function

    Private Sub CreateServerNode()
        Dim FormConnectToServer As New FormConnectToServer()
        FormConnectToServer.ShowDialog()

        If FormConnectToServer.Successful Then
            Dim root As New TreeNode

            root.ImageIndex = TreeNodeImageIndex.SQLServerServer
            root.SelectedImageIndex = TreeNodeImageIndex.SQLServerServerSelected
            tv.Nodes.Add(root)

            Dim DatabaseObject As New DatabaseServer
            DatabaseObject.DatabaseType = DatabaseType.SQLServer

            If FormConnectToServer.cboAuthenticationType.Text.ToUpper = "WINDOWS AUTHENTICATION" Then
                DatabaseObject.AuthenticationType = DatabaseAuthenticationType.WindowsAuthentication
            Else
                DatabaseObject.AuthenticationType = DatabaseAuthenticationType.DatabaseAuthentication
                DatabaseObject.Login = FormConnectToServer.txtLoginName.Text
                DatabaseObject.Password = FormConnectToServer.txtPassword.Text
            End If
            DatabaseObject.ServerName = FormConnectToServer.cboServerName.Text
            root.Text = FormConnectToServer.cboServerName.Text

            DatabaseObject.Name = "Server"
            DatabaseObject.Type = DatabaseObjectType.Server
            root.Tag = DatabaseObject

            Dim blank As New TreeNode
            Dim blankDatabaseObject As New DatabaseObject
            blankDatabaseObject.Type = DatabaseObjectType.None
            blank.Tag = blankDatabaseObject
            root.Nodes.Add(blank)
        Else
            Exit Sub
        End If
        FormConnectToServer = Nothing



    End Sub

    Private Sub tv_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tv.BeforeExpand

        Dim CurrentNodeDatabaseObject As DatabaseObject = e.Node.Tag
        Dim CurrentNode As TreeNode = e.Node
        If CurrentNodeDatabaseObject Is Nothing Then
            Exit Sub
        End If

        Select Case CurrentNodeDatabaseObject.Type
            Case DatabaseObjectType.Server
                If CurrentNode.Nodes.Count = 1 Then
                    Dim ChildNode As TreeNode = CurrentNode.Nodes(0)
                    Dim ChildNodeDatabaseObject As DatabaseObject = ChildNode.Tag
                    If ChildNodeDatabaseObject.Type = DatabaseObjectType.None Then
                        Dim DatabaseServer As DatabaseServer = CurrentNodeDatabaseObject

                        Dim Connection As New SqlConnection(DatabaseServer.ConnectionString)
                        Try
                            Connection.Open()
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        End Try

                        Dim sql As String = "Select @@VERSION"

                        Dim Command As New SqlCommand(sql, Connection)
                        Dim Version As String = Command.ExecuteScalar

                        Version = Replace(Version, "  ", " ")
                        Dim VersionKeys() As String = Split(Version, " ")
                        Dim VersionNumber As String = VersionKeys(3)

                        Select Case VersionNumber
                            Case "2000"
                                sql = "Select name, dbid as database_ID from master.dbo.sysdatabases order by Name"
                            Case "2005", "2008", "2012"
                                sql = "Select Name,database_ID from master.sys.databases order by Name"
                            Case Else
                                MsgBox("Unknown version :  " & VersionNumber)
                        End Select

                        Debug.WriteLine(sql)
                        Command = New SqlCommand(sql, Connection)
                        CurrentNode.Nodes.Clear()
                        Dim Reader As SqlDataReader = Command.ExecuteReader
                        Do While Reader.Read
                            Dim tn As New TreeNode
                            tn.Text = Reader("Name")
                            tn.ImageIndex = TreeNodeImageIndex.SQLServerDatabase
                            tn.SelectedImageIndex = TreeNodeImageIndex.SQLServerDatabaseSelected
                            Dim DatabaseObject As New DatabaseObject
                            DatabaseObject.Name = tn.Text
                            DatabaseObject.Type = DatabaseObjectType.Database
                            DatabaseObject.Version = VersionNumber
                            tn.Tag = DatabaseObject
                            e.Node.Nodes.Add(tn)

                            'Add empty node placeholder here.
                            Dim tnBlank As New TreeNode
                            tnBlank.Text = ""
                            DatabaseObject = New DatabaseObject
                            DatabaseObject.Type = DatabaseObjectType.None
                            tnBlank.Tag = DatabaseObject
                            tn.Nodes.Add(tnBlank)
                        Loop
                        Connection.Close()

                    End If
                End If
            Case DatabaseObjectType.Database
                If CurrentNode.Nodes.Count() = 1 Then
                    Dim ChildNode As TreeNode = CurrentNode.Nodes(0)
                    Dim ChildNodeDatabaseObject As DatabaseObject = ChildNode.Tag
                    If ChildNodeDatabaseObject.Type = DatabaseObjectType.None Then
                        CurrentNode.Nodes.Clear()

                        Dim ConnectionString As String = GetConnectionStringFromTreeNode(CurrentNode)

                        Dim Connection As New SqlConnection(ConnectionString)
                        Connection.Open()

                        'Add Schema's Aware.
                        Dim sql As String = ""
                        If CurrentNodeDatabaseObject.Version = "2000" Then
                            sql = "select * from [" & CurrentNode.Text & "].dbo.sysobjects where xtype in ('U','V') order by xtype,name"
                        Else
                            sql = "SELECT TABLE_SCHEMA SName, TABLE_NAME Name FROM [" + CurrentNode.Text + "].INFORMATION_SCHEMA.TABLES union SELECT TABLE_SCHEMA SName, TABLE_NAME Name FROM [" + CurrentNode.Text + "].INFORMATION_SCHEMA.Views"
                            'select o.*, QUOTENAME(SCHEMA_NAME( s.schema_id )) SName from [" & CurrentNode.Text & "].dbo.sysobjects o inner join sys.schemas s on s.schema_id = o.uid where o.xtype in ('U','V') order by SName, o.name"
                        End If

                        Debug.WriteLine(sql)

                        Dim Command As New SqlCommand(sql, Connection)

                        Dim Reader As SqlDataReader
                        Try
                            Reader = Command.ExecuteReader
                        Catch ex As Exception
                            MsgBox(ex.Message)
                            Exit Sub
                        End Try

                        Do While Reader.Read
                            Dim DBO As New DatabaseObject
                            DBO.Type = DatabaseObjectType.Table
                            DBO.Name = Reader("Name").ToString

                            If CurrentNodeDatabaseObject.Version = "2000" Then
                                DBO.Schema = "dbo"
                            Else
                                DBO.Schema = Reader("SName").ToString
                            End If


                            Dim tn As New TreeNode
                            tn.Text = DBO.Schema & "." & DBO.Name
                            tn.Tag = DBO
                            tn.ImageIndex = TreeNodeImageIndex.SQLServerTable
                            tn.SelectedImageIndex = TreeNodeImageIndex.SQLServerTableSelected
                            e.Node.Nodes.Add(tn)

                        Loop
                        Connection.Close()
                    End If
                End If
            Case DatabaseObjectType.Table
            Case DatabaseObjectType.Column
            Case DatabaseObjectType.None
            Case DatabaseObjectType.Configuration
                'do nothing

        End Select

        Debug.WriteLine("m")
        If False Then
        End If
    End Sub

    Private Function GetConnectionStringFromTreeNode(ByVal TreeNode As TreeNode) As String
        Dim CurrentNode As TreeNode = TreeNode
        Do While CurrentNode Is Nothing = False
            Dim DatabaseObject As DatabaseObject = CurrentNode.Tag
            If DatabaseObject.Type = DatabaseObjectType.Server Then
                Dim DatabaseServer As DatabaseServer = DatabaseObject
                Return DatabaseServer.ConnectionString
            Else
                'do nothing
            End If
            CurrentNode = CurrentNode.Parent
        Loop
        Return ""
    End Function

    Private Function GetServerNameFromTreeNode(ByVal TreeNode As TreeNode) As String
        Dim CurrentNode As TreeNode = TreeNode
        Do While CurrentNode.Parent Is Nothing
            Dim DatabaseObject As DatabaseObject = CurrentNode.Tag
            If DatabaseObject.Type = DatabaseObjectType.Server Then
                Dim DatabaseServer As DatabaseServer = DatabaseObject
                Return DatabaseServer.ServerName
            Else
                'do nothing
            End If
            CurrentNode = CurrentNode.Parent
        Loop
        Return ""
    End Function

    Private Sub tv_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tv.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            SetSelectedNodeByPosition(tv, e.X, e.Y)
            Dim tn As TreeNode = tv.SelectedNode
            If tn Is Nothing = False Then
                Dim dbo As DatabaseObject = tn.Tag
                Select Case dbo.Type
                    Case DatabaseObjectType.Table
                        ShowContextMenu(ContextMenuStrip2, Me.Left + e.X, Me.Top + e.Y)
                End Select
            End If
        End If
    End Sub

    Private Sub ShowContextMenu(ByVal Ctx As ContextMenuStrip, ByVal x As Long, ByVal y As Long)
        Ctx.Show(x, MousePosition.Y + 9)
    End Sub

    Public Sub SetSelectedNodeByPosition(ByVal tv As TreeView, ByVal mouseX As Integer, ByVal mouseY As Integer)
        Dim node As TreeNode
        Try
            Dim pt As New Point(mouseX, mouseY)
            tv.PointToClient(pt)
            node = tv.GetNodeAt(pt)
            tv.SelectedNode = node

        Catch ex As Exception

        End Try
    End Sub

    Private Sub TestToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem1.Click
        Dim DatabaseObject As DatabaseObject = tv.SelectedNode.Tag
        If DatabaseObject Is Nothing = False Then
            If DatabaseObject.Type = DatabaseObjectType.Table Then
                Dim dt As New DataTabPage
                dt.Text = DatabaseObject.Schema & "." & DatabaseObject.Name
                dt.LoadGrid(tv.SelectedNode)
                TabControl1.Controls.Add(dt)
                TabControl1.SelectedTab = dt

            End If
        End If
    End Sub

    Private Sub cmdPickDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPickDirectory.Click
        Dim DialogResult As DialogResult = FolderBrowserDialog1.ShowDialog()
        If DialogResult = Windows.Forms.DialogResult.OK Then
            txtPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub ConnectToServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToServerToolStripMenuItem.Click
        CreateServerNode()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub


    Private Sub cmdAddToTemplates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddToTemplates.Click

        Dim Result As String = InputBox("Enter a new Name", "New Project")
        If Len(Result) > 0 Then
            Dim csi As ConnectServerItem = CreateNewCSI()
            csi.ProjectName = Result
            Servers.Add(csi)
            cboProject.Items.Add(csi.ProjectName)
            SaveCSI()
        End If
    End Sub

    Private Sub cboCodeType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCodeType.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.CodeType = cboCodeType.SelectedValue
        End If
    End Sub

    Private Sub cboCommentLevel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCommentLevel.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.CommentLevel = cboCommentLevel.SelectedValue
        End If
    End Sub

    Private Sub cboKeyPassing_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboKeyPassing.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.KeyPassing = cboKeyPassing.SelectedValue
        End If
    End Sub

    Private Sub cboGridCodingStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboGridCodingStyle.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.GridCodingStyle = cboGridCodingStyle.SelectedValue
        End If
    End Sub

    Private Sub cboConnectionStringType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboConnectionStringType.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.ConnectionStringStyle = cboConnectionStringType.SelectedValue
        End If
    End Sub

    Private Sub txtPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPath.TextChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.ProjectPath = txtPath.Text
        End If
    End Sub

    Private Sub chkIncludeSourceCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkIncludeSourceCode.CheckedChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.SourceCodeFiles = chkIncludeSourceCode.Checked
        End If
    End Sub

    Private Sub chkIncludeWebConfigFile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkIncludeWebConfigFile.CheckedChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.WebConfigFile = chkIncludeWebConfigFile.Checked
        End If

    End Sub

    Private Sub txtConnectionString_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtConnectionString.TextChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.ConnectionStringName = txtConnectionString.Text
        End If
    End Sub

    Private Sub cboPaging_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPaging.SelectedIndexChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.Paging = cboPaging.SelectedValue
        End If
    End Sub

    Private Sub chkSorting_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSorting.CheckedChanged
        If IsNothing(CurrentCSI) = False Then
            CurrentCSI.Sorting = chkSorting.Checked
        End If
    End Sub

    Public Sub SaveCSI()
        Dim xmlDoc As New XmlDocument
        Dim xmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "")
        xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement)

        Dim RootNode As XmlElement = xmlDoc.CreateElement("PCG")
        RootNode.SetAttribute("Version", "1.0")
        xmlDoc.AppendChild(RootNode)

        For Each csi As ConnectServerItem In Servers
            Dim Server As XmlElement = xmlDoc.CreateElement("Project")
            Server.SetAttribute("ProjectName", csi.ProjectName)
            Server.SetAttribute("ProjectPath", csi.ProjectPath)
            Server.SetAttribute("CodeType", csi.CodeType)
            Server.SetAttribute("CommentLevel", csi.CommentLevel)
            Server.SetAttribute("KeyPassing", csi.KeyPassing)
            Server.SetAttribute("GridCodingStyle", csi.GridCodingStyle)
            Server.SetAttribute("SourceCodeFiles", csi.SourceCodeFiles)
            Server.SetAttribute("WebConfigFile", csi.WebConfigFile)
            Server.SetAttribute("ConnectionStringStyle", csi.ConnectionStringStyle)
            Server.SetAttribute("ConnectionStringName", csi.ConnectionStringName)
            Server.SetAttribute("Paging", csi.Paging)
            Server.SetAttribute("Sorting", csi.Sorting)
            RootNode.AppendChild(Server)
        Next
        xmlDoc.Save("Project.xml")
    End Sub

    Private Sub txtProjectName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProjectName.TextChanged
        If CurrentCSI Is Nothing = False Then
            CurrentCSI.ProjectName = txtProjectName.Text
        End If

    End Sub

    Private Sub cboProject_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProject.TextChanged
        For Each csi As ConnectServerItem In Servers
            If csi.ProjectName.ToUpper = cboProject.Text.ToUpper Then
                CurrentCSI = csi

                Try
                    txtProjectName.Text = csi.ProjectName
                Catch ex As Exception

                End Try
                Try
                    cboCodeType.SelectedValue = csi.CodeType
                Catch ex As Exception

                End Try

                Try
                    cboCommentLevel.SelectedValue = csi.CommentLevel
                Catch ex As Exception

                End Try

                Try
                    cboGridCodingStyle.SelectedValue = csi.GridCodingStyle
                Catch ex As Exception

                End Try

                Try
                    cboKeyPassing.SelectedValue = csi.KeyPassing
                Catch ex As Exception

                End Try

                Try
                    cboConnectionStringType.SelectedValue = csi.ConnectionStringStyle
                Catch ex As Exception

                End Try

                Try
                    txtPath.Text = csi.ProjectPath
                Catch ex As Exception

                End Try

                Try
                    txtConnectionString.Text = csi.ConnectionStringName
                Catch ex As Exception

                End Try

                Try
                    chkIncludeSourceCode.Checked = csi.SourceCodeFiles
                Catch ex As Exception

                End Try

                Try
                    chkIncludeWebConfigFile.Checked = csi.WebConfigFile
                Catch ex As Exception

                End Try

                Try
                    cboPaging.SelectedValue = csi.Paging
                Catch ex As Exception

                End Try

                Try
                    chkSorting.Checked = csi.Sorting
                Catch ex As Exception

                End Try

                CurrentCSI = csi
                Exit For
            End If
        Next
    End Sub

    Private Sub cboProject_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboProject.SelectedIndexChanged
        For Each csi As ConnectServerItem In Servers
            If csi.ProjectName.ToUpper.Trim = cboProject.Text.ToUpper.Trim Then
                CurrentCSI = csi
                LoadConnectServerItem(csi)

            End If
        Next
    End Sub

    Private Sub ClassGenerationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClassGenerationToolStripMenuItem.Click
        Dim DatabaseObject As DatabaseObject = tv.SelectedNode.Tag
        If DatabaseObject Is Nothing = False Then
            If DatabaseObject.Type = DatabaseObjectType.Table Then
                Dim dt As New ClassTabPage
                dt.Text = DatabaseObject.Name
                dt.LoadGrid(tv.SelectedNode)
                TabControl1.Controls.Add(dt)
                TabControl1.SelectedTab = dt
            End If
        End If

    End Sub

    Private Sub DragDropGenerationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DragDropGenerationToolStripMenuItem.Click
        Dim DatabaseObject As DatabaseObject = tv.SelectedNode.Tag
        If DatabaseObject Is Nothing = False Then
            If DatabaseObject.Type = DatabaseObjectType.Table Then
                Dim dt As New DDTabPage
                dt.Text = DatabaseObject.Name
                dt.LoadGrid(tv.SelectedNode)
                TabControl1.Controls.Add(dt)
                TabControl1.SelectedTab = dt
            End If
        End If
    End Sub

    Private Sub WCFServiceGenerationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WCFServiceGenerationToolStripMenuItem.Click
        Dim DatabaseObject As DatabaseObject = tv.SelectedNode.Tag
        If DatabaseObject Is Nothing = False Then
            If DatabaseObject.Type = DatabaseObjectType.Table Then
                Dim dt As New WCFClassTabPage
                dt.Text = DatabaseObject.Name
                dt.LoadGrid(tv.SelectedNode)
                TabControl1.Controls.Add(dt)
                TabControl1.SelectedTab = dt
            End If
        End If

    End Sub

End Class
